package com.huawei.crm.sample.adapter.inbound.rest.order.converter;

import com.huawei.crm.sample.adapter.inbound.rest.order.vo.OrderVO;
import com.huawei.crm.sample.application.order.dto.OrderDTO;
import com.huawei.crm.sample.application.order.gateway.CustomerGateway;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;

import java.util.List;

@Mapper(componentModel = "spring")
public interface OrderVOConverter {

    @Mapping(target = "items", source = "items")
    OrderVO toVO(OrderDTO dto);
    
    @Mapping(target = "items", source = "items")
    OrderDTO toDTO(OrderVO vo);

    OrderVO.OrderItemVO toItemVO(OrderDTO.OrderItemDTO dto);
    OrderDTO.OrderItemDTO toItemDTO(OrderVO.OrderItemVO vo);

    List<OrderVO.OrderItemVO> toItemVOList(List<OrderDTO.OrderItemDTO> dtos);
    List<OrderDTO.OrderItemDTO> toItemDTOList(List<OrderVO.OrderItemVO> vos);

    default OrderVO toVOWithCustomerName(OrderDTO dto, CustomerGateway customerGateway) {
        OrderVO vo = toVO(dto);
        if (vo != null && dto != null) {
            vo.setCustomerName(customerGateway.getCustomerNameById(dto.getCustomerId()));
        }
        return vo;
    }
} 